package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.GetDeptListReqDto;
import com.yixun.qfbao.apidto.GetDeptListResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.DepartmentDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.dao.DepartmentDao;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.GetDeptListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class GetDeptListHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetDeptListHander.class);
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    PartnerInfoService partnerInfoService;
    @Autowired
    CompanyService companyService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetDeptListReqVo req = (GetDeptListReqVo)reqDto;
        GetDeptListReqDto reqDto1 = req.getParams();
        BaseResponseDTO<GetDeptListResDto> res = new BaseResponseDTO<GetDeptListResDto>();
        GetDeptListResDto resDto = new GetDeptListResDto();
        try {
            DepartmentDto departmentDto = new DepartmentDto();
            departmentDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
            departmentDto.setMerchantId(reqDto1.getMerchantId());
            Long storeId = reqDto1.getStoreId();
            List<Department> list;
            if(storeId != null) {
            	departmentDto.setParentId(storeId);
            	list = departmentDao.selectListByDto(departmentDto);
            } else {
                // 特殊逻辑: 分销组别报表,需要筛选出当前用户公司的所有组别.
                // 通过菜单点进来,没有公司Id参数.通过登录用户信息获取一遍.
                if(Objects.isNull(reqDto1.getMerchantId()) && StringUtils.isNotBlank(reqDto1.getUserId())){
                    PartnerInfo partnerInfo = partnerInfoService.selectById(Long.valueOf(reqDto1.getUserId()));
                    if(Objects.nonNull(partnerInfo)){
                        Company company = companyService.selectById(partnerInfo.getHigherid());
                        if(company.getCompanyType().compareTo(1L) == 0){
                            departmentDto.setMerchantId(company.getId());
                        }
                    }
                }

            	departmentDto.setParentId(0L);
                List<Department> merchants = departmentDao.selectListByDto(departmentDto);
                if(merchants !=null && merchants.size() > 0){
                    departmentDto.setParentId(merchants.get(0).getId());
                }
                if(reqDto1.getParentId() != null){
                    departmentDto.setParentId(null);
                    departmentDto.setParentId(reqDto1.getParentId());
                }
                list = departmentDao.selectListByDto(departmentDto);
            }
            resDto.setList(list);

        }catch(TServerException e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg(e.getErrorMsg());
        } catch (Exception e) {
            logger.error("GetDeptListHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("系统异常");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }

}
